package com.feixiang.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.feixiang.bean.User;
import com.feixiang.dao.UserDao;

@SuppressWarnings("unchecked")
@Repository
public class UserDaoImpl implements UserDao {
	@Autowired
	private HibernateTemplate hibernateTemplate;
	public int addUser(User user) {
		String result = this.hibernateTemplate.save(user).toString();
		if(result != null){
			return Integer.parseInt(result);
		}
		return 0;
	}
	
	public List<User> getAllUsers() {
		
		List<User> users = new ArrayList<User>();
		try {
			users = this.hibernateTemplate.find("from User");
		} catch (DataAccessException e) {
			System.out.println("�־ò� UserDaoImpl.getAllUsers()�����쳣!");
		}
		return users;
	}

	public User findUserByName(String name) {
		List<User> users  = this.hibernateTemplate.find("from User where loginName=?", name);
		if(users!=null && !users.isEmpty()){
			return users.get(0);
		}
		return null;
	}

}
